//
//  PersonDisplayCell.m
//  CloudKaraoke
//
//  Created by BHDMinh on 4/21/14.
//  Copyright (c) 2014 soncamedia. All rights reserved.
//

#import "PersonDisplayCell.h"
#import "Musician.h"
#import "Singer.h"
//#import "SongType.h"
//#import "Language.h"

@implementation PersonDisplayCell

- (void)awakeFromNib
{
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void) configureDisplayCellForPerson:(id)person searchString:(NSString *)searchText
{
    
    NSString *name = @"";
    NSString *titleRaw = @"";
    NSString *shortname = @"";
    NSString *songCount = @"";
    NSData *coverData = nil;
    
    if([person isKindOfClass:[Musician class]]){
        name = [(Musician *)person name];
        titleRaw = [(Musician *)person titleRaw];
        shortname = [(Musician *)person shortname];
        songCount = [NSString stringWithFormat:@"%d", [(Musician *)person songCount]];
        coverData = [(Musician *)person coverData];
    }else if([person isKindOfClass:[Singer class]])
    {
        name = [(Singer *)person name];
        titleRaw = [(Singer *)person titleRaw];
        shortname = [(Singer *)person shortname];
        songCount = [NSString stringWithFormat:@"%d", [(Singer *)person songCount]];
        coverData = [(Singer *)person coverData];
    }
    /*
    else if([person isKindOfClass:[SongType class]])
    {
        name = [(SongType *)person name];
//        titleRaw = [(SongType *)person titleRaw];
        shortname = [(SongType *)person shortname];
//        songCount = [SongType stringWithFormat:@"%d", [(Singer *)person songCount]];
//        coverData = [(SongType *)person coverData];
    }
    else if([person isKindOfClass:[Language class]])
    {
        name = [(Language *)person name];
        //        titleRaw = [(SongType *)person titleRaw];
        shortname = [(Language *)person shortname];
        //        songCount = [SongType stringWithFormat:@"%d", [(Singer *)person songCount]];
        //        coverData = [(SongType *)person coverData];
    }
    */
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:name];
    NSRange searchRange = [titleRaw rangeOfString:searchText options:NSCaseInsensitiveSearch];
    if (searchRange.location == NSNotFound) {
        searchRange = [shortname rangeOfString:searchText options:NSCaseInsensitiveSearch];
        if (searchRange.location != NSNotFound) {
            NSArray * strArray = [titleRaw componentsSeparatedByString:@" "];
            NSInteger pos = 0;
            for(NSInteger i= 0; i < searchRange.location; i++)
            {
                pos += [[strArray objectAtIndex:i] length] + 1;
            }
            
            for (NSInteger i = searchRange.location; i < searchRange.length; i++) {
                [attrString addAttribute:NSForegroundColorAttributeName value:KOK_COLOR_SEARCH_HIGHLIGHT range:NSMakeRange(pos, 1)];
                
                pos += [[strArray objectAtIndex:i] length] + 1;
            }
        }else {
            searchRange = [name rangeOfString:searchText options:NSCaseInsensitiveSearch];
            if(searchRange.location != NSNotFound) {
                [attrString addAttribute:NSForegroundColorAttributeName value:KOK_COLOR_SEARCH_HIGHLIGHT range:searchRange];
            }
        }
    }else {
        [attrString addAttribute:NSForegroundColorAttributeName value:KOK_COLOR_SEARCH_HIGHLIGHT range:searchRange];
    }
    [self.labelTitle setTextColor:KOK_COLOR_TITLE];
    self.labelTitle.attributedText = attrString;
    if (coverData != nil) {
        [self.imgCover setImage:[UIImage imageWithData:coverData]];
    }else {
        [self.imgCover setImage:[UIImage imageNamed:@"ico-user.png"]]; 
    }
    
    [_lableSongCount setText:songCount];
}

@end
